루비에서 상속은 단순히 메서드를 공유하는 것 이상입니다. 그것은 상태의 진화입니다. 예를 들어, KaraokeSong < Song과 같은 서브클래스를 생성할 때, 자식 클래스는 부모 클래스의 구조를 상속하면서 자신의 정체성을 정교하게 다듬는 '은-것이다'(is-a) 관계를 설정합니다.
1. 초기화 체인
그리고 super 키워드는 세대 간의 다리를 연결합니다. initialize메서드 내부에서 super 를 호출하면 인수를 부모의 생성자로 전달하여 기본 인스턴스 변수(@name, @artist)가 먼저 설정되고, 서브클래스가 고유한 상태(@lyrics)를 추가할 수 있도록 보장합니다.
2. 메서드 증강
예를 들어, to_s 와 같은 메서드를 오버라이드하면 동작을 확장할 수 있습니다. 새로운 정의 내에서 super 를 호출함으로써 부모의 문자열 출력을 캡처하고, 단순히 서브클래스의 새 데이터를 추가함으로써 깔끔한 진화적 흐름을 유지할 수 있습니다.
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What is the primary function of the
super keyword in a subclass's initialize method?It clones the parent class into the child.
It passes arguments to the parent class's version of the method.
It deletes the instance variables of the parent.
It creates a global reference to the superclass.
✅ Correct!
Correct! super allows the parent class to handle the initialization of state it 'owns' before the child adds its own specific logic.❌ Incorrect
Recall that super is used to invoke the same-named method in the superclass.QUESTION 2
What happens to instance variables of a parent class during inheritance?
They are private and hidden from the subclass.
They are automatically populated even if 'super' is not called.
They become part of the subclass's state once the parent's initialize logic runs.
They are converted into class variables.
✅ Correct!
Instance variables are tied to the object; once initialized by the parent's code (via super), they exist within the subclass instance.❌ Incorrect
Instance variables aren't automatically populated; the initialization logic that sets them must be executed, usually via super.QUESTION 3
If you override
to_s in a subclass but do NOT call super, what happens?The parent's
to_s is still called automatically.The subclass implementation completely replaces the parent's implementation.
Ruby throws a SystemStackError.
The object defaults back to the standard
Object#to_s format.✅ Correct!
In Ruby, method overriding is total unless you explicitly use super to augment the parent's behavior.❌ Incorrect
Ruby does not automatically merge methods; it follows the lookup chain and stops at the first match.QUESTION 4
Which relationship best describes subclassing?
$Has-a$
$Is-a$
Contains-a
Provides-a
✅ Correct!
Inheritance defines an 'is-a' relationship (e.g., a KaraokeSong is a Song).❌ Incorrect
Composition is 'has-a'; inheritance is 'is-a'.QUESTION 5
What does the
inspect method reveal about an object's state?Only the public methods available.
The raw internal instance variables and their current values.
The source code of the class.
A list of all subclasses.
✅ Correct!
inspect is a debugging tool that shows the object's ID and all initialized instance variables.❌ Incorrect
inspect focuses on state (variables), not behavior (methods).State Evolution Analysis
Encapsulation vs. Tight Coupling
Consider a version of KaraokeSong where the developer implements to_s by directly interpolating the parent's variables: 'Song: #@name--#@artist (#@duration) [#@lyrics]'. Reading Context: We're correctly displaying the value of the @lyrics instance variable... The idea of a karaoke version of 'My Way' that lasts for 3,750 minutes is just too frightening to consider.
Q
So why is this a bad way to implement to_s? [Referencing the implementation of KaraokeSong#to_s that directly accesses parent instance variables]
Solution:
This is a bad implementation because it creates **tight coupling**. By directly accessing @name, @artist, and @duration (instance variables of the parent Song class), the subclass KaraokeSong becomes dependent on the internal implementation details of the parent. If the parent class changes its variable names (e.g., from @name to @title), the subclass breaks even though its own logic didn't change. It is better to use 'super' or call the parent's attribute methods to maintain **encapsulation** and ensure that changes in the parent class are automatically reflected in the subclass.
This is a bad implementation because it creates **tight coupling**. By directly accessing @name, @artist, and @duration (instance variables of the parent Song class), the subclass KaraokeSong becomes dependent on the internal implementation details of the parent. If the parent class changes its variable names (e.g., from @name to @title), the subclass breaks even though its own logic didn't change. It is better to use 'super' or call the parent's attribute methods to maintain **encapsulation** and ensure that changes in the parent class are automatically reflected in the subclass.
Q
How does using the 'super' keyword solve the issue described above?
Solution:
By using 'super' within the subclass's to_s method, you delegate the responsibility of formatting the basic song information back to the Song class. This ensures that if the Song class changes how it displays itself (e.g., changing the separator from '--' to ' by '), KaraokeSong automatically inherits that change without needing any manual code updates.
By using 'super' within the subclass's to_s method, you delegate the responsibility of formatting the basic song information back to the Song class. This ensures that if the Song class changes how it displays itself (e.g., changing the separator from '--' to ' by '), KaraokeSong automatically inherits that change without needing any manual code updates.